package com.yidu.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnAuditMapper;
import com.yidu.dao.SnBranchMapper;
import com.yidu.dao.SnComSaDetailMapper;
import com.yidu.dao.SnComSalesMapper;
import com.yidu.dao.SnMaPurchaseMapper;
import com.yidu.dao.SnProductionMapper;
import com.yidu.dao.SnRetailMapper;
import com.yidu.model.SnAudit;
import com.yidu.model.SnBranch;
import com.yidu.model.SnComSaDetail;
import com.yidu.model.SnComSales;
import com.yidu.model.SnMaPurchase;
import com.yidu.model.SnProduction;
import com.yidu.model.SnRetail;
import com.yidu.model.SnUser;
import com.yidu.service.SnAuditService;
/**
 * 审核实现类
 * @author chensituo
 * @date 2018年11月29日 上午2:03:32
 */
@Service
public class SnAuditServiceImpl implements SnAuditService{
	/**
	 * 注入审核mapper
	 */
	@Resource
	SnAuditMapper auditMapper;
	/**
	 *注入生产计划mapper
	 */
	@Resource
	SnProductionMapper productionMapper;
	/**
	 * 注入材料mapper
	 */
	@Resource
	SnMaPurchaseMapper maPurChaseMapper;
	
	@Resource
	SnBranchMapper branchMapper;

	/**
	 * 注入零售退货mapper
	 */
	@Resource
	SnComSalesMapper comSalesMapper;
	/**
	 * 注入零售退货详情mapper
	 */
	@Resource
	SnComSaDetailMapper comsSaDetailMapper;
	/**
	 * 注入零售mapper
	 */
	@Resource
	SnRetailMapper retailMapper;
	@Override
	public int addBranch(String braId) {
		SnBranch branch=new SnBranch();
		branch.setBraId(braId);
		branch.setSort(2);
		branchMapper.updateByPrimaryKeySelective(branch);
		
		SnAudit audit=new SnAudit();
		audit.setAuId(Tools.getDateOrderNo());
		audit.setAllId(braId);
		audit.setOptime(Tools.getCurDateTime());
		audit.setAuState(0);
		audit.setAuComment("加盟审核");
		return auditMapper.insertSelective(audit);
	}	
	
	@Override
	public int addProduction(String proId) {
		SnProduction production=new SnProduction();
		production.setProId(proId);
		production.setProState(1);
		productionMapper.updateByPrimaryKeySelective(production);
		
		SnAudit audit=new SnAudit();
		audit.setAuId(Tools.getDateOrderNo());
		audit.setAllId(proId);
		audit.setOptime(Tools.getCurDateTime());
		audit.setAuState(0);
		audit.setAuComment("生产审核");
		return auditMapper.insert(audit);
	}

	@Override
	public List<SnAudit> findProAll(Map<String, Object> maps) {
		return auditMapper.findProAll(maps);
	}

	@Override
	public int findProCount(Map<String, Object> maps) {
		return auditMapper.findProCount(maps);
	}

	@Override
	public int addProAudit(SnAudit audit, SnUser user) {
		//调用修改的方法
		int rows=auditMapper.updateByPrimaryKeySelective(audit);
		
		SnAudit snAudit=new SnAudit();
		snAudit.setAuId(Tools.getDateOrderNo());
		snAudit.setAllId(audit.getAuId());
		snAudit.setAuComment(audit.getAuComment());
		snAudit.setAuIdea(audit.getAuIdea());
		snAudit.setAuState(audit.getAuState());
		snAudit.setOptime(Tools.getCurDateTime());
		snAudit.setOper(user.getUserName());
		int row=auditMapper.insertSelective(snAudit);
		if(snAudit.getAuState()!=null && !"".equals(snAudit.getAuState())) {
			if(snAudit.getAuState()==3 || snAudit.getAuState()==4) {
				System.out.println("----------------打回-----------------");
				SnProduction production=new SnProduction();
				production.setProId(audit.getAllId());
				production.setProState(0);
				//调用生产计划修改的方法
				productionMapper.updateByPrimaryKeySelective(production);
			}
			
			if("总经理".equals(audit.getAuComment()) && audit.getAuState()==2) {
				System.err.println("**************审核通过***************");
				SnProduction production=new SnProduction();
				production.setProId(audit.getAllId());
				production.setProState(2);
				//调用生产计划修改的方法
				productionMapper.updateByPrimaryKeySelective(production);
			}
			
		}
		
		
		
		if(row==1 && rows==1) {
			return 1;
		}else {
			return 0;
		}
		
	}
	
	@Override
	public int addMaAudit(SnAudit audit, SnUser user) {
		//调用修改的方法
		int rows=auditMapper.updateByPrimaryKeySelective(audit);
		
		SnAudit snAudit=new SnAudit();
		snAudit.setAuId(Tools.getDateOrderNo());
		snAudit.setAllId(audit.getAuId());
		snAudit.setAuComment(audit.getAuComment());
		snAudit.setAuIdea(audit.getAuIdea());
		snAudit.setAuState(audit.getAuState());
		snAudit.setOptime(Tools.getCurDateTime());
		snAudit.setOper(user.getUserName());
		int row=auditMapper.insertSelective(snAudit);
		if(snAudit.getAuState()!=null && !"".equals(snAudit.getAuState())) {
			if(snAudit.getAuState()==3 || snAudit.getAuState()==4) {
				System.out.println("-------------------打回-------------------");
				SnMaPurchase maPurchase=new SnMaPurchase();
				maPurchase.setPurId(audit.getAllId());
				maPurchase.setOper("0");
				maPurChaseMapper.updateByPrimaryKeySelective(maPurchase);
			}
			
			if("总经理".equals(audit.getAuComment()) && audit.getAuState()==2) {
				System.err.println("**************审核通过***************");
				SnMaPurchase maPurchase=new SnMaPurchase();
				maPurchase.setPurId(audit.getAllId());
				maPurchase.setOper("2");
				maPurChaseMapper.updateByPrimaryKeySelective(maPurchase);
			}
		}
		
		
		
		if(row==1 && rows==1) {
			return 1;
		}else {
			return 0;
		}
		
	}

	@Override
	public List<SnAudit> ProQuery(Map<String, Object> maps) {
		return auditMapper.ProQuery(maps);
	}

	@Override
	public int ProQueryCount(Map<String, Object> maps) {
		return auditMapper.ProQueryCount(maps);
	}

	@Override
	public List<SnAudit> findProAllSum(Map<String, Object> maps) {
		return auditMapper.findProAllSum(maps);
	}

	@Override
	public int findProCountSum(Map<String, Object> maps) {
		return auditMapper.findProCountSum(maps);
	}
	
	@Override
	public List<Map<String, Object>> chaseAuditAll(Map<String, Object> maps) {
		//查询采购财务审核所有
		return auditMapper.chaseAuditAll(maps);
	}

	@Override
	public int chaseAuditCount(Map<String, Object> maps) {
		//查询采购财务审核行数
		return auditMapper.chaseAuditCount(maps);
	}

	@Override
	public List<Map<String, Object>> chaseAuditSumAll(Map<String, Object> maps) {
		//查询采购总经理审核所有
		return auditMapper.chaseAuditSumAll(maps);
	}

	@Override
	public int chaseAuditCountSum(Map<String, Object> maps) {
		///查询采购总经理审核行数
		return auditMapper.chaseAuditCountSum(maps);
	}

	@Override
	public List<Map<String, Object>> retailAuditAll(Map<String, Object> maps) {
		return auditMapper.retailAuditAll(maps);
	}

	@Override
	public int retailAuditCount(Map<String, Object> maps) {
		return auditMapper.retailAuditCount(maps);
	}

	@Override
	public int addRetailAudit(SnAudit audit, SnUser user) {
		//调用审核修改的方法
		int rows=auditMapper.updateByPrimaryKeySelective(audit);
		//调用根据审核id查询退货id的方法
		String csId=auditMapper.findRetailAuId(audit.getAuId());
		//调用根据退货id查询退货详情的方法
		List<SnComSaDetail> list=comsSaDetailMapper.findCsId(csId);
		
		//创建审核记录model
		SnAudit snAudit=new SnAudit();
		snAudit.setAuId(Tools.getDateOrderNo());
		snAudit.setAllId(audit.getAuId());
		snAudit.setAuComment(audit.getAuComment());
		snAudit.setAuIdea(audit.getAuIdea());
		snAudit.setAuState(audit.getAuState());
		snAudit.setOptime(Tools.getCurDateTime());
		snAudit.setOper(user.getUserName());
		//调用增加审核的方法
		int row=auditMapper.insertSelective(snAudit);
		//定义一个变量
		Integer money=0;
		for (SnComSaDetail snComSaDetail : list) {
			//将退货详情的退货数量* 商品零售价
			money+=snComSaDetail.getSdAlready()*snComSaDetail.getCiRetail().intValue();
		}
		//如果状态不为空
		if(snAudit.getAuState()!=null && !"".equals(snAudit.getAuState())) {
			//根据审核业务id查询退货表
			SnComSales comSales=comSalesMapper.selectByPrimaryKey(audit.getAllId());
			//根据退货业务id查询零售表
			SnRetail snRetail=retailMapper.selectByPrimaryKey(comSales.getAllId());
			//如果状态是2 过或5
			if(snAudit.getAuState()==2 || snAudit.getAuState()==5) {
				System.out.println("-------------------打回-------------------");
				snRetail.setReState(1);
				//调用零售修改的方法
				retailMapper.updateByPrimaryKeySelective(snRetail);
			}else {
				System.err.println(money);
				//如果总金额大于5000
				if(money>5000) {
					System.out.println("------------------总经理审核------------------");
					//如果不是总经理审核
					if(!"总经理".equals(audit.getAuComment())){
						audit.setAuState(3);
						//调用修改的方法
						auditMapper.updateByPrimaryKeySelective(audit);
					}
							
					
					//如果是总经理审核通过
					if("总经理".equals(audit.getAuComment()) && audit.getAuState()==4) {
						System.err.println("**************总经理审核通过***************");
						snRetail.setReState(4);
						//调用修改零售表的方法
						retailMapper.updateByPrimaryKeySelective(snRetail);
					}
						
				}else {
					System.out.println("--------------------审核完成-----------------------");
					snRetail.setReState(4);
					//调用修改零售表的方法
					retailMapper.updateByPrimaryKeySelective(snRetail);
				}
			}
			
		}
		
		
		if(row==1 && rows==1) {
			return 1;
		}else {
			return 0;
		}
		
	}

	@Override
	public List<Map<String, Object>> retailAuditAllSum(Map<String, Object> maps) {
		return auditMapper.retailAuditAllSum(maps);
	}

	@Override
	public int retailAuditCountSum(Map<String, Object> maps) {
		return auditMapper.retailAuditCountSum(maps);
	}
	

}
